var ws,uid,icon,nick,type_id, myuid,group_name,isSame,hxId,hxIcon,hxNick;
var app = angular.module('app',[]);
var type = 2;
app.controller('myController',function ($scope,$http) {
	$scope.initView = function(){
		$scope.self_uid = myuid;
		$http({
			method: 'post',
			url: apiRoot,
			data: {
				action: 'User.getUserInfoByTelOrOpenID',
				uid:uid
			}
		}).then(function (result){
			if(result.data.error == 0){
				var info = result.data.data;	
				var user = info['user'];/*个人信息*/  
				var start = info.follow;/*粉丝数和关注数*/
				$scope.uid = user.id;
				$scope.fans_num = start.info;  //粉丝数
				$scope.follow_num = start.follow;  //关注人数
				var gen = ['男','女'];  
				$scope.nick = user.nick;  //昵称  				
				$scope.gender = gen[user.gender];  //性别
				$scope.location = (user.location !=0 ? user.location : ' ');
				$scope.icon = getAvatar(user.icon);		//用户图像
				
				hxId = user.mobile;
				hxIcon = $scope.icon
				hxNick = $scope.nick;
//				console.log(hxId+"#######"+hxIcon+"#######"+hxNick);
				$scope.isSign = user.sign;  //签约
				$scope.follow = user.follow;
				$.each(info['group'], function(key,gro) {
					info['group'][key]['icon'] = getPic(gro['icon']);
				});
				$scope.group = info['group'];   //群组
				var group = info['group'];
				$scope.group_name = group.group_name;
				$scope.group_icon = group.icon;
				if(info['group'].length>0){
					$scope.groupPrompt = 0;  //该用户没有建群的时候显示“没有数据”
					if(info['group'].length>=3){
						$scope.groupMore = 1;//该用户群的数据大于三条的时候显示“查看更多”
					} else{
						$scope.dynamicMore = 0;//该用户群的数据小于三条的时候不显示“查看更多”
					}
				}else{
					$scope.groupPrompt = 1;  //该用户有群则不显示“没有数据”
					$scope.dynamicMore = 0;//该用户群的数据小于三条的时候不显示“查看更多”
				}				
				$scope.dynamic = info['dynamic'];/*动态*/
				if(info['dynamic'].length>0){
					$scope.dynamicPrompt = 0;//该用户有动态的时候不显示“没有数据”
					if(info['dynamic'].length>=3){
						$scope.dynamicMore = 1;//该用户动态的数据大于三条的时候显示“查看更多”
					} else{
						$scope.dynamicMore = 0;//该用户动态的数据小于三条的时候不显示“查看更多”
					}
				}else{
					$scope.dynamicPrompt = 1;//该用户没有动态的时候显示“没有数据”
					$scope.dynamicMore = 0;//该用户动态的数据小于三条的时候不显示“查看更多”
				}
				
				$.each(info['goods'], function(key,vo) {
					info['goods'][key]['good_image'] = getPic(vo['good_image']);
				});
				
				$scope.goods = info['goods'];/*商品*/
				if(info['goods'].length > 0){
					$scope.goodsPrompt = 0; //不显示提示“没有数据”
					if(info['goods'].length>=3){
						$scope.goodsMore = 1; //当数据大于3时，显示“查看更多”
					} else{
						$scope.goodsMore = 0;//当数据小于3时，不显示“查看更多”
					}
				} else {
					$scope.goodsPrompt = 1;//显示提示“没有数据”
					$scope.goodsMore = 0;//当数据小于3时，不显示“查看更多”
				}
			}else{
				toast(data.desc || '沒有数据');
			}
		},function () {
			errortoast();
		})
		
		//判断是否是关注的状态	
		$http({
			method:'post',
			url:apiRoot,
			data:{
				action:'Community.checkFollowStatus',
				uid:myuid,
				type:2,			
				type_id:uid
			}
		}).then(function successCallback(response){
			plus.nativeUI.closeWaiting();
			if(response.data.error == 121){
				$scope.attention = 121;
			}else{
				$scope.attention = 115;/*显示取消关注*/
			}	
			$scope.already = 1;
		},function errorfunction(e){ 
			console.log(JSON.stringify(e));
		})
	};	
//	console.log(hxId+"#######"+hxIcon+"#######"+hxNick);               
	$scope.honerChat = function(){
       if(isSame == '1'){      
            mui.back();
            window.plus.bridge.execSync("community","backNews","");
       }else{
            window.plus.bridge.exec("community", "sendMessage", [hxId,hxIcon,hxNick]);
//     		window.plus.bridge.exec("community", "sendMessage", [$scope.huanxinId+'',$scope.huanxinIcon,$scope.huanxinNick]);
       	}
  	}
	  
	
	/*关注/取消关注*/
	var is_clickAttention = true;
	$scope.isattention = function(way){	
		if(is_clickAttention){
		plus.nativeUI.showWaiting('');
		$http({
			method:'post',
			url:apiRoot,
			data:{
				action:'Community.follow',
				uid:myuid,
				type:2,				
				type_id:uid
			}
		}).then(function successCallback(response){
			plus.nativeUI.closeWaiting();
			console.log(JSON.stringify(response));
			if(response.data.error == 115){
				/*操作成功*/
				if(way == 2){
					$scope.attention = 121;/*显示关注*/
					toast("已取消关注");
				}else{
					$scope.attention = 115;/*显示取消关注*/
					toast("已关注");
				}				
			}
		},function errorfunction(e){ 
			errortoast();
			console.log(JSON.stringify(e));
		})	
		is_clickAttention = false;
		setTimeout(function(){
			is_clickAttention = true;
		},2000)
		}		
	}	
	
	
	$scope.comein = function (uid,type,id) {
		plus.webview.create('view_detail.html','view_detail.html',{},{noteUid:uid,type:type,noteId:id}).show('pop-in');
	}	
	
	$scope.lookGood = function (uid,type,id) {
		openPage('goods_detail.html',{gUid:uid,type:type,gId:id});/*查看商品*/
	}
	
	$scope.groupList = function () {
		goNewPage('my_talk.html',{uid:uid});/*查看群群 列表*/		
	}
	
	$scope.dynamicList = function () {
		goNewPage('somebody_dynamic_state.html',{uid:uid});/*更多动态*/
	}	
	
	$scope.GoodsList = function () {
		plus.webview.create('somebody_goods.html','somebody_goods.html',{},{uid:uid}).show('pop-in');/*更多商品*/
	}	
	//跳转到举报页面
	$scope.report = function (uid,type) {
		console.log(uid+"#####"+type);
		plus.webview.create('report.html','report.html',{},{reportUid:uid,reportType:type}).show('pop-in');/*更多商品*/
	}
	//打开粉丝列表
	$scope.openFansList = function (uid,name,is_fans,type) {
		plus.webview.create('user_fans.html','user_fans.html',{},{uid:uid,Uname:name,is_fans:is_fans,is_type:type}).show('pop-in');/*更多商品*/
	}

})

//预加载个人页面
//function showHoner(userid,img,nick){    
//  var hornerP = plus.webview.create('honer_page.html', 'honer_page.html',{},{icon_id:userid,icon_img:img,icon_name:nick,isSame:'1'});
//  hornerP.show();
//} 
 
function ready_honer(){
	ws = plus.webview.currentWebview();
	myuid = plus.storage.getItem('uid');
	uid = ws.icon_id;/*用户id*/
	type_id = ws.type_id;
	isSame = ws.isSame; 
//	console.log(isSame);
	var allocation = document.querySelector('[ng-controller=myController]');
	$scope = angular.element(allocation).scope();
	$scope.initView();
	$scope.$apply();	
}

if(window.plus){
	ready_honer();
}else{
	document.addEventListener('plusready',ready_honer,false);
}